local BattleScene = class("BattleScene")

local battleSceneLoader = require "Battle/BattleSceneLoader"

require "Battle/Battle"
require "Battle/RoleAttribute"
require "Battle/BattleData"
require "Battle/BattleConfig"
require "Battle/BattleRole"
require "Battle/RoleManager"
require "Battle/BattleArray"
require "Battle/BattleCal"
require "Battle/RoundManager"
require "Battle/ActionPlayer"
require "Battle/BattleSort"
require "Battle/FlyWordBar"
require "Battle/BattleMainUI"
require "Battle/BattleCurtainUI"
require "Battle/BattleResultUI"

require "Config/Actions"
require "Config/RoleConfig"
require "Config/Beiji"
require "Config/BattleConfigData"

function BattleScene:ctor(...)
	self.state = BattleState.Init
    MusicMgr.Instance:PlayBg("bg_02")
	Battle.setBattleScene(self)
end

function BattleScene.Start(gameObject)
	local object = BattleScene.new()

	UpdateBeat:Add(object.update, object)
	return object
end

function BattleScene.Destroy(gameObject, object)
	UpdateBeat:Remove(object.update, object)
	object = nil
end


function BattleScene:update()
	--GameLog.Log("BattleScene:update")

	if(self.state == BattleState.Init) then
		self:StateInit()
	elseif (self.state == BattleState.LoadBg) then
		self:StateLoadBg()
	elseif (self.state == BattleState.LoadFormation) then
		self:StateLoadFormation()
	elseif (self.state == BattleState.LoadRole) then
		self:StateLoadRole()
	elseif (self.state == BattleState.InitBattle) then
		self:StateInitBattle()
	elseif (self.state == BattleState.StartShow) then
		self:StateStartShow()
	elseif (self.state == BattleState.Round) then
		self:StateRound()
	elseif (self.state == BattleState.EndShow) then
		self:StateEndShow()
	elseif (self.state == BattleState.End) then
		self:StateEnd()
	elseif(self.state ==BattleState.WaitBackToBenzhen) then

	end

end

function BattleScene:StateInit()
	ActionPlayer.init()
	Battle.init()
	BattleData.init()
	BattleConfig.init()
	RoleManager.init()
	RoundManager.init()

	self.startShowPlayed = false
	self.endShowPlayed = false
	self.state = BattleState.LoadBg
    FlyWordBar.init()
end

function BattleScene:StateLoadBg()
	if(not self.startCurtainPlayed) then
        self.startCurtainPlayed = true
	    local bg = battleSceneLoader.loadBg("Battle/BackGround/BgRoot")
        BattleCurtainUI:StartCurtain(function (args) self.state = BattleState.LoadFormation end)
    end
end

function BattleScene:StateLoadFormation()
	local _config = battleSceneLoader.loadFormation()
	BattleConfig.registerRoot(_config)
	self.state = BattleState.LoadRole
end

function BattleScene:StateLoadRole()
	for i=1,#BattleData.roles do
		--GameLog.Warning(BattleData.roles[i].res)
		local res = battleSceneLoader.loadRole(BattleData.roles[i].res)
		local role = BattleRole.new(BattleData.roles[i], res, true)
        role.transform.gameObject:SetActive(false)
		RoleManager.addFriend(role)
	end

	for i=1,#BattleData.enemys do
		--GameLog.Warning(BattleData.enemys[i].res)
		local res = battleSceneLoader.loadRole(BattleData.enemys[i].res)
		local role = BattleRole.new(BattleData.enemys[i], res, false)
        role.transform.gameObject:SetActive(false)
		role:flpY()
		RoleManager.addEnemy(role)
	end

	self.state = BattleState.InitBattle
    BattleMainUI.init()
end



function BattleScene:StateInitBattle()
	BattleArray.arrangeFriend(RoleManager.team)
	BattleArray.arrangeEnemy(RoleManager.enemy)
	--local leftPosTbl = BattleConfig.getFormationPos(true, BattleData.leftFormationId)

	for i,v in ipairs(RoleManager.team) do
		v:playBaseAnim("idle")
		v:registerUpdate()
	end

	for i,v in ipairs(RoleManager.enemy) do
		v:playBaseAnim("idle")
		v:registerUpdate()
	end

	self.state = BattleState.StartShow
end

function BattleScene:StateStartShow()
	if(not self.startShowPlayed) then
		self.startShowPlayed = true
        local  time = 0
	    for i=1,#RoleManager.team do
--	    	GameLog.Warning("left role i = " .. i)
	    	local unit = RoleManager.team[i]
            unit.transform.gameObject:SetActive(true)
            unit:play(unit.entrance.name)
            if( unit.entrance.time > time) then
                time = unit.entrance.time
            end
	    end
        local timer = nil
        local battleStartEffect;
--        GameLog.Error("play left entrance time = " .. time)
	    timer = Timer.New(function()		
--        GameLog.Error("play PlayEntrance loop =  " .. timer.loop)
        if(timer.loop == 3) then
--            GameLog.Error("play left entrance over ")
            time = 0
	        for i=1,#RoleManager.enemy do
	        	local unit = RoleManager.enemy[i]
                unit.transform.gameObject:SetActive(true)
                unit:play(unit.entrance.name)
                if( unit.entrance.time > time) then
                    time = unit.entrance.time
                end
	        end
            timer.duration = time
--            GameLog.Error("play right entrance time = " .. time)
        elseif(timer.loop == 2) then 
            battleStartEffect = ObjectPoolManager:GetObjectForce("BattleStart")
--            GameLog.Error("play right entrance over ")
            timer.duration = 1
        elseif(timer.loop == 1) then 
            UnityUtil.Destroy(battleStartEffect.gameObject)
--            GameLog.Error("play battle start effect  ")
--            BattleCurtainUI:EndCurtain()
	        self.state = BattleState.Round
        end end, time, 3, false)
	    timer:Start()	
	end
end

function BattleScene:StateRound()
	RoundManager.update()
end

function BattleScene:StateEndShow()
	if(not self.endShowPlayed) then
        BattleCurtainUI:EndCurtain(function () self.state = BattleState.End end)
		self.endShowPlayed = true
	end
end

function BattleScene:StateEnd()
	-- change to benzhen
	if(RoleManager.team ~= nil) then
		for i,v in ipairs(RoleManager.team) do
			v:unregisterUpdate()
		end
	end

	if(RoleManager.deadTeam ~= nil) then
		for i,v in ipairs(RoleManager.deadTeam) do
			v:unregisterUpdate()
		end
	end

	if(RoleManager.enemy ~= nil) then
		for i,v in ipairs(RoleManager.enemy) do
			v:unregisterUpdate()
		end
	end

	if(RoleManager.deadEnemy ~= nil) then
		for i,v in ipairs(RoleManager.deadEnemy) do
			v:unregisterUpdate()
		end
	end

	GameLog.Warning("BattleScene:StateEnd")

	RoundManager.clear()
	ActionPlayer.clear()
	Battle.clear()
	BattleConfig.clear()
	BattleData.clear()
	RoleManager.clear()
    BattleMainUI.clear()
    FlyWordBar.clear()
    BattleCurtainUI.clear()
	
	if(not self.battleresiltShow) then
		self.battleresiltShow = true
        BattleResultUI:Show(function () 
        	GameLog.Error("OnClick ButtonBack")
        	UnityEngine.SceneManagement.SceneManager.LoadScene("Benzhen");
			end)
	end

	self.state = BattleState.WaitBackToBenzhen
end

return BattleScene